﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.SceneManagement;

[XLua.Hotfix]
public class SceneMgr : MonoSingleton<SceneMgr>
{
    public void LoadScene(string path)
    {
        Resources.UnloadUnusedAssets();
#if UNITY_EDITOR
        path = "Scenes/" + path;
#elif UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE
        ResourceManager.Instance.UnloadAsset();
        ResourceManager.Instance.LoadAssetWithDependenciesEx("scenes");
#endif
        SceneManager.LoadScene(path);
    }

    public void LoadSceneAnsc(string path, Action finish = null)
    {
        StartCoroutine(LoadSceneAnscEx(path, finish));
    }

    private IEnumerator LoadSceneAnscEx(string path, Action finish)
    {
        Resources.UnloadUnusedAssets();
#if UNITY_EDITOR
        path = "Scenes/" + path;
#elif UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE
        ResourceManager.Instance.UnloadAsset();
        yield return ResourceManager.Instance.LoadAssetWithDependenciesExAnsc("scenes");
#endif
        yield return SceneManager.LoadSceneAsync(path);
        if (finish != null)
        {
            finish();
        }
    }
}
